package com.qf.flash.mvp.presenter;

import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.http.HttpManager;
import com.qf.flash.http.response.JoinTeamBean;
import com.qf.flash.model.Pay2SponsorBean;
import com.qf.flash.model.PayParam;
import com.qf.flash.mvp.contract.StartTeamContract;
import com.qf.flash.utils.LogUtil;
import com.qf.flashlib.base.BasePresenter;
import com.qf.flashlib.base.BaseSubscriber;
import com.qf.flashlib.sharedpreferences.SavePermanentSharePrefs;
import com.qf.flashlib.sharedpreferences.SharePrefConstants;
import com.qf.flashlib.util.RxUtils;

import java.util.HashMap;
import java.util.Map;

public class StartTeamPresenter extends BasePresenter<StartTeamContract.View> implements StartTeamContract.Presenter {

    @Override
    public void startTeam(Pay2SponsorBean bean) {
        if (bean == null) return;
        String areaId = SavePermanentSharePrefs.getInstance().getDataString(SharePrefConstants.AREA_ID);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", UserMgr.getInstance().getLoginUser().getUid());
//        map.put("payType", payType);
        map.put("startTime", bean.getStartTime());
        map.put("teamName", bean.getTeamName());
        map.put("password", bean.getPassword());
        map.put("num", bean.getNum());
        map.put("playId", bean.getPlayId());
        map.put("playType", bean.getPlayType());
        map.put("money", bean.getMoney());
        map.put("areaId", areaId);
        map.put("isShopEnter", bean.getIsShopEnter());
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                .START_TEAM(map)
                .compose(RxUtils.<PayParam>handleResult())
                .compose(RxUtils.<PayParam>io_main())
                .subscribeWith(new BaseSubscriber<PayParam>(mRootView) {
                    @Override
                    public void onSuccess(PayParam param) {
                        mRootView.startSuccess(param);
                    }
                })
        );
    }

    @Override
    public void joinTeam(String orderId) {
        String uid = UserMgr.getInstance().getLoginUser().getUid();
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                .JOIN_TEAM(uid, orderId, -1)
                .compose(RxUtils.handleResult())
                .compose(RxUtils.io_main())
                .subscribeWith(new BaseSubscriber<JoinTeamBean>(mRootView) {
                    @Override
                    public void onSuccess(JoinTeamBean param) {
                        mRootView.startSuccess(null);
                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);
                        LogUtil.d(" getMessage: "+t.getMessage());
                    }


                })
        );
    }
}
